﻿using System;
using System.Text;

namespace IosDecode
{



    public static class NewDecode
    {

        private static readonly byte[] EncodedToPlain = new byte[256];


        private static void Load()
        {
            EncodedToPlain[103] = 97;
            EncodedToPlain[120] = 98;
            EncodedToPlain[56] = 99;
            EncodedToPlain[118] = 100;
            EncodedToPlain[102] = 101;
            EncodedToPlain[117] = 102;
            EncodedToPlain[53] = 103;
            EncodedToPlain[122] = 104;
            EncodedToPlain[52] = 105;
            EncodedToPlain[76] = 106;
            EncodedToPlain[101] = 107;
            EncodedToPlain[87] = 108;
            EncodedToPlain[121] = 109;
            EncodedToPlain[55] = 110;
            EncodedToPlain[70] = 111;
            EncodedToPlain[51] = 112;
            EncodedToPlain[113] = 113;
            EncodedToPlain[65] = 114;
            EncodedToPlain[84] = 115;
            EncodedToPlain[112] = 116;
            EncodedToPlain[107] = 117;
            EncodedToPlain[97] = 118;
            EncodedToPlain[108] = 119;
            EncodedToPlain[66] = 120;
            EncodedToPlain[86] = 121;
            EncodedToPlain[110] = 122;
            EncodedToPlain[79] = 65;
            EncodedToPlain[77] = 66;
            EncodedToPlain[69] = 67;
            EncodedToPlain[78] = 68;
            EncodedToPlain[80] = 69;
            EncodedToPlain[68] = 70;
            EncodedToPlain[81] = 71;
            EncodedToPlain[75] = 72;
            EncodedToPlain[67] = 73;
            EncodedToPlain[104] = 74;
            EncodedToPlain[83] = 75;
            EncodedToPlain[85] = 76;
            EncodedToPlain[57] = 77;
            EncodedToPlain[109] = 78;
            EncodedToPlain[82] = 79;
            EncodedToPlain[106] = 80;
            EncodedToPlain[98] = 81;
            EncodedToPlain[73] = 82;
            EncodedToPlain[49] = 83;
            EncodedToPlain[100] = 84;
            EncodedToPlain[90] = 85;
            EncodedToPlain[88] = 86;
            EncodedToPlain[105] = 87;
            EncodedToPlain[111] = 88;
            EncodedToPlain[72] = 89;
            EncodedToPlain[89] = 90;
            EncodedToPlain[99] = 48;
            EncodedToPlain[74] = 49;
            EncodedToPlain[116] = 50;
            EncodedToPlain[50] = 51;
            EncodedToPlain[115] = 52;
            EncodedToPlain[48] = 53;
            EncodedToPlain[114] = 54;
            EncodedToPlain[71] = 55;
            EncodedToPlain[54] = 56;
            EncodedToPlain[119] = 57;


            //EncodedToPlain[46] = 123;
            //EncodedToPlain[32] = 125;
            //EncodedToPlain[44] = 40;
            //EncodedToPlain[61] = 41;
            //EncodedToPlain[41] = 46;
            //EncodedToPlain[37] = 58;
            //EncodedToPlain[125] = 32;
            //EncodedToPlain[42] = 95;
            //EncodedToPlain[35] = 44;
            //EncodedToPlain[123] = 10;
            //EncodedToPlain[58] = 13;
            //EncodedToPlain[43] = 61;
            //EncodedToPlain[10] = 45;
            //EncodedToPlain[40] = 59;
            //EncodedToPlain[45] = 37;
            //EncodedToPlain[95] = 124;
            //EncodedToPlain[124] = 42;
            //EncodedToPlain[59] = 43;
            //EncodedToPlain[13] = 35;

            EncodedToPlain[46] = 35;//123;
            EncodedToPlain[32] = 43;//125;
            EncodedToPlain[44] = 42;//40;
            EncodedToPlain[61] = 124;//41;
            EncodedToPlain[41] = 37;//46;
            EncodedToPlain[37] = 59;//58;
            EncodedToPlain[125] = 45;//32;
            EncodedToPlain[42] = 61;//95;
            EncodedToPlain[35] = 13;//44;
            EncodedToPlain[123] = 10;//10;
            EncodedToPlain[58] = 44;//13;
            EncodedToPlain[43] = 95;//61;
            EncodedToPlain[10] = 32;//45;
            EncodedToPlain[40] = 58;//59;
            EncodedToPlain[45] = 46;//37;
            EncodedToPlain[95] = 41;//124;
            EncodedToPlain[124] = 40;//42;
            EncodedToPlain[59] = 125;//43;
            EncodedToPlain[13] = 123;// 35; 


        }


        private static int isKeyChar(char c)
        {
            int result; // r0@3
            int v3; // r1@4

            if (((c - 48) & 0xFF) >= 0xA && (c - 65) >= 0x1Au)
            {
                v3 = 1;
                if (((c - 97) & 0xFF) >= 0x1A)
                    v3 = 0;
                result = v3;
            }
            else
            {
                result = 1;
            }
            return result;
        }

        private static int isKeySymbol(char c)
        {
            int v2; // r2@3
            int v4; // r1@12

            if (c > 0x5Eu)
            {
                if (c != 95)
                    goto LABEL_9;
                return 1;
            }
            if (c > 0x3Cu)
            {
                if (c != 61)
                    goto LABEL_9;
                return 1;
            }
            v2 = (c - 10) & 0xFF;


            var a = Convert.ToBoolean((1 << (c - 10)) & 0xA400009);

            if (v2 <= 0x1B && a)
                return 1;
        LABEL_9:
            if (((c - 40) & 0xFF) < 7 || (c - 58) < 2u)
                return 1;
            v4 = 1;
            if (((c - 123) & 0xFF) >= 3)
                v4 = 0;
            return v4;
        }



        private static int isKeySymbol(int i)
        {
            int v2; // r2@3 

            if (i > 0x5Eu)
            {
                if (i != 95)
                    return LABEL_9(i);
                return 1;
            }

            if (i > 0x3Cu)
            {
                if (i != 61)
                    return LABEL_9(i);
                return 1;
            }
            v2 = (i - 10) & 0xFF;
            if (v2 <= 0x1B && Convert.ToBoolean((1 << (i - 10)) & 0xA400009))
                return 1;

            return 0;
        }


        private static int LABEL_9(int i)
        {
            int o = 1;

            if (((i - 40) & 0xFF) < 7 || (i - 58) < 2u)
                o = 1;
            if (((i - 123) & 0xFF) >= 3)
                o = 0;
            return o;
        }


    

        /// <summary>
        ///     解密方法
        /// </summary>
        public static string ToDecode(string v)
        {
            Load();
            var r = new char[v.Length];
            for (var i = 0; i < v.Length; ++i)
            {
                var c = v[i];

                if (isKeyChar(c) == 1)
                {
                    r[i] = (char)EncodedToPlain[c];
                }
                else
                {
                    if (isKeySymbol(c) == 1)
                    {
                        r[i] = (char)EncodedToPlain[c - 3];
                    }
                    else
                    {
                        var v12 = (char)(c ^ 0x6F);
                       // if (isKeyChar(v12) == 0)
                            r[i] = v12;
                    }
                }
            }
            return new string(r);
        }
    }
}